package com.qingchi.server.controller;

import com.socialuni.social.model.common.ResultVO;
import com.qingchi.base.model.account.DevAccountDO;
import com.qingchi.base.modelVO.dev.DevUserDetailQueryVO;
import com.qingchi.base.modelVO.dev.DevUserVO;
import com.qingchi.base.repository.openData.DevAccountRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Optional;

/**
 * @author qinkaiyuan
 * @date 2019-02-17 14:14
 */
@RestController
@RequestMapping("devUser")
public class DevUserQueryController {
    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    private DevAccountRepository devAccountRepository;

    @PostMapping("queryDevUserDetail")
    @ResponseBody
    public ResultVO<DevUserVO> appLogin(@RequestBody @Valid @NotNull DevUserDetailQueryVO loginVO, Integer devId) {
        if (!devId.equals(1)) {
            return new ResultVO<>("不允许授权");
        }
        //根据秘钥查询
        Optional<DevAccountDO> devAccountDOOptional = devAccountRepository.findFirstBySecretKey(loginVO.getThreeSecretKey());
        //存在返回，不存在提示错误
        return devAccountDOOptional.map(devAccountDO -> new ResultVO<>(new DevUserVO(devAccountDO))).orElseGet(() -> new ResultVO<>("不允许授权1"));
    }

}
